#!/bin/bash
mkdir /home/elk/elasticsearch/config/ -p
mkdir /home/elk/logstash/config/ -p
mkdir /home/elk/logstash/pipeline/ -p
mkdir /home/elk/kibana/config/ -p

cat >/etc/sysctl.conf <<EOF
vm.max_map_count = 262144
EOF

sysctl -p

cat >/home/elk/elasticsearch/config/elasticsearch.yml <<EOF
discovery.type: single-node
network.host: 0.0.0.0
xpack:
  ml.enabled: false
  monitoring.enabled: false
  security.enabled: false
  watcher.enabled: false
EOF

docker run -d -it -p 9200:9200 -p 9300:9300  \
-v /home/elk/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /home/elk/elasticsearch/config/jvm.options:/usr/share/elasticsearch/config/jvm.options \
--name elasticsearch \
elasticsearch:6.4.3

docker cp elasticsearch:/usr/share/elasticsearch/config/jvm.options /home/elk/elasticsearch/config/jvm.options

docker cp ./elasticsearch-analysis-ik-6.4.3.zip elasticsearch:/usr/share/elasticsearch/plugins/elasticsearch-analysis-ik-6.4.3.zip

mkdir /usr/share/elasticsearch/plugins/ik/ -p

cat >/home/elk/logstash/config/logstash.yml <<EOF
config:
  reload:
    automatic: true
    interval: 3s
xpack:
  management.enabled: false
  monitoring.enabled: false
EOF

cat >/home/elk/logstash/pipeline/pipelines.yml <<EOF
- pipeline.id: test
  path.config: "/usr/share/logstash/pipeline/logstash-test.conf"
EOF

cat >/home/elk/logstash/pipeline/logstash-test.conf <<EOF
input {
    file {
        path => ["/usr/share/logstash/pipeline/logs/test.log"]
        start_position => "beginning"
    }
}

filter {
  mutate {
    gsub => ["message", "\r", ""]
  }
  dissect {
    mapping => {"message" => "%{date} %{+date} [%{task} %{+task}] [%{type}] %{class} - %{info}"}
  }
}

output {
    elasticsearch { hosts => ["192.168.2.143:9200"] }
    stdout { codec => rubydebug }
}
EOF

docker run -d -it -v /home/elk/logstash/pipeline/:/usr/share/logstash/pipeline/ \
 -p 3456:3456 \
 --name logstash \
 -v /home/elk/logstash/config/:/usr/share/logstash/config/ logstash:6.4.3

 docker run -d -it \
 --name logstash \
logstash:6.4.3

cat >/home/elk/kibana/config/kibana.yml <<EOF
server.host: "0.0.0.0"
elasticsearch.url: http://192.168.2.143:9200
xpack:
  apm.ui.enabled: false
  graph.enabled: false
  ml.enabled: false
  monitoring.enabled: false
  reporting.enabled: false
  security.enabled: false
  grokdebugger.enabled: false
  searchprofiler.enabled: false
EOF

docker run -d -it -p 5601:5601 -v /home/elk/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml \
 --name kibana \
 kibana:6.4.3


docker run -d \
  --name=elasticsearch-head \
  --restart=always \
  -p 9100:9100 \
  mobz/elasticsearch-head:5-alpine

docker run -d --name=logstash logstash:6.4.3
docker cp logstash:/usr/share/logstash/config /home/elk/logstash/
docker cp logstash:/usr/share/logstash/data  /home/elk/logstash/
docker cp logstash:/usr/share/logstash/pipeline  /home/elk/logstash/
docker rm -f logstash

docker cp foodie-items.sql logstash:/usr/share/logstash/config
docker cp logstash-db-sync.conf logstash:/usr/share/logstash/config
docker cp mysql-connector-java-5.1.30.jar logstash:/usr/share/logstash/config

chmod 777 /home/elk/logstash/

docker cp logstash-db-sync.conf logstash:/usr/share/logstash/pipeline/logstash-db-sync.conf